Option Strict On

Imports Excel = Microsoft.Office.Interop.Excel
'Imports Office = Microsoft.Office.Core

Public Class TemplateTable
    Implements ITemplateTable

    Private table As DataTable
    Private location As Excel.Range

    Public Sub New(ByVal tableName As String, ByRef dSet As DataSet, ByRef range As Excel.Range)
        Me.location = range
        If (dSet Is Nothing) Then
            Throw New ArgumentException("DataSet object was not supplyed!")
        End If

        table = dSet.Tables(tableName)
        If (table Is Nothing) Then
            Throw New ArgumentException( _
                String.Format("There are no Table with name: '{0}' in supplyed DataSet object!", tableName))
        End If

        Me._referenceAddress = Me.location.Worksheet.Range(location, location.Offset(0, ColumnCount)).AddressLocal
    End Sub

    Public ReadOnly Property ColumnCount() As Integer Implements ITemplateTable.ColumnCount
        Get
            Return table.Columns.Count
        End Get
    End Property

    Public Sub DeployTable() Implements ITemplateTable.DeployTable
        Dim i As Int32
        Dim j As Int32
        Dim topLeft As Excel.Range = location

        If (Not table.Rows.Count = 0) Then
            For i = table.Rows.Count - 1 To 1 Step -1
                For j = 0 To table.Columns.Count - 1
                    RangeEvaluator.SetValue( _
                        topLeft.Offset(0, j), _
                        table.Rows(i)(j))
                Next
                topLeft.Worksheet.Range(topLeft, topLeft.Offset(0, table.Columns.Count - 1)). _
                    Insert(Excel.XlDirection.xlDown, True)

                topLeft = topLeft.Offset(-1, 0)
            Next
            For j = 0 To table.Columns.Count - 1
                topLeft.Offset(0, j).Value = table.Rows(0)(j)
            Next
        Else
            location.Value = ""
        End If

        Me._IsDeployed = True
    End Sub

    Public Function HasIntersection(ByRef range As Microsoft.Office.Interop.Excel.Range) As Boolean Implements ITemplateTable.HasIntersection
        Try
            Dim o As Object = range.Application.Range(range.AddressLocal & " " & ReferenceAddress).Value
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private _IsDeployed As Boolean = False
    Public ReadOnly Property IsDeployed() As Boolean Implements ITemplateTable.IsDeployed
        Get
            Return Me._IsDeployed
        End Get
    End Property

    Public Function IsReferenceOnMe(ByVal areaReference As String) As Boolean Implements ITemplateTable.IsReferenceOnMe
        Try
            Dim o As Object = location.Worksheet.Range(areaReference & " " & ReferenceAddress).Value
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private _referenceAddress As String
    Public ReadOnly Property ReferenceAddress() As String Implements ITemplateTable.ReferenceAddress
        Get
            Return Me._referenceAddress
        End Get
    End Property

    Public ReadOnly Property RowCount() As Integer Implements ITemplateTable.RowCount
        Get
            Return table.Rows.Count
        End Get
    End Property
End Class
